<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>dynamicWater</title>
    <link rel="stylesheet" href="./css/style.css" />
    <script type="text/javascript" src="../../lib/aircity/ac_conf.js"></script>
    <script type="text/javascript" src="../../lib/aircity/ac.min.js"></script>
    <script type="text/javascript" src="../../lib/vue3.js"></script>
  </head>
  <body>
    <div id="app">
      <div id="player" style="height: 100%"></div>
      <div class="control">
        <div class="top">
          <div>淹没分析</div>
        </div>
        <div class="params">
          <div class="button-box">
            <button @click="start()">开始淹没分析</button>
          </div>

          <div class="button-box">
            <button @click="stop()" class="sunlight stop">停止淹没分析</button>
          </div>

          <div class="title">参数设置</div>
          <div class="item">
            <div class="label">
              <div>水位高度</div>
              <div style="color: #b6b6cf">({{ elevation }}m)</div>
            </div>
            <div class="content">
              <div class="range">
                <input type="range" v-model="elevation" :min="-1" :max="100" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script type="text/javascript">
      // 按需获取
      const { createApp, reactive, watch, ref } = Vue

      // vue组件
      const vueComposition = {
        setup() {
          // 初始化y轴高度
          const elevation = ref(-1)
          const coordinates = [
            [475000, 2445000],
            [475000, 2498000],
            [555000, 2498000],
            [555000, 2445000]
          ]
          /**
           * 开始淹没分析
           */
          const start = () => {
            // y轴高度
            const y = Number(elevation.value)
            let waterObj = {
              id: 'water', //湖水的id
              coordinates: coordinates.map(item => [...item, y]), //构成湖水区块的数据
              style: 0 //  动态水的样式，取值：0（深蓝）、1（蓝）、2（湖水）
            }
            // 添加水面
            __g.dynamicWater.add(waterObj, null)
          }

          /**
           * 淹没分析高度更新
           */
          const update = () => {
            // y轴高度
            const y = Number(elevation.value)
            let waterObj = {
              id: 'water', //湖水的id
              coordinates: coordinates.map(item => [...item, y]), //构成湖水区块的数据
              style: 0 //  动态水的样式，取值：0（深蓝）、1（蓝）、2（湖水）
            }
            // 更新水面
            __g.dynamicWater.update(waterObj, null)
          }

          // elevation监听，开启更新事件
          watch(elevation, () => {
            update()
          })

          /**
           * 停止淹没分析
           */
          const stop = () => {
            // 关闭分析
            __g.dynamicWater.clear()
          }

          return {
            elevation,
            start,
            stop
          }
        }
      }

      // 挂载vue
      const app = createApp(vueComposition).mount('#app')

      /**
       * 初始化三维
       * @constructor
       */
      const OnLoad = () => {
        new DigitalTwinPlayer(HostConfig.Player, {
          domId: 'player' //DOM元素
        }).getAPI() // getAPI后可全局调用__g
      }

      /**
       * 自适应分辨率
       * @returns {Promise<void>}
       */
      const onResize = async () => {
        let playerView = document.getElementById('player')
        playerView.style.height = window.innerHeight + 'px'
        playerView.style.width = window.innerWidth + 'px'
      }

      // 页面加载调用
      window.addEventListener('load', OnLoad, true)

      // 页面变换调用
      window.addEventListener('resize', onResize, true)

      //页面离开事件(离开时清除此页面的操作及重置场景)
      window.onunload = function (e) {
        __g.waterMesh.clear()
        // 重置场景
        __g.reset()
      }
    </script>
  </body>
</html>
